﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;

namespace Songhay.OpenXml
{
    /// <summary>
    /// Static members for Open XML.
    /// </summary>
    public static partial class OpenXmlUtility
    {
        /// <summary>
        /// Generates and adorns the <see cref="DocumentFormat.OpenXml.Packaging.DocumentSettingsPart"/>
        /// for Microsoft Office Word 2010 Document packages.
        /// </summary>
        /// <param name="package">The package.</param>
        /// <remarks>
        /// This procedure actually identifies the package as an Office Word 2010 document.
        /// It prevents Word from dropping into Compatibility Mode when the document is opened.
        /// It is based on code generated by the Reflect Code command
        /// in the Open XML SDK 2.0 Productivity Tool for Microsoft Office.
        /// </remarks>
        public static void GenerateOfficeDocumentSettings(WordprocessingDocument package)
        {
            if(package == null) throw new ArgumentNullException("package", "The Word Document is null.");
            if(package.MainDocumentPart == null) throw new ArgumentNullException("package.MainDocumentPart", "The Main Document Part is null.");

            var part = package.MainDocumentPart.AddNewPart<DocumentSettingsPart>("rId4");

            var settings = new Settings() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14" } };
            settings.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            settings.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            settings.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            settings.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            settings.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            settings.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            settings.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            settings.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
            settings.AddNamespaceDeclaration("sl", "http://schemas.openxmlformats.org/schemaLibrary/2006/main");

            var compatibility = new Compatibility();
            CompatibilitySetting compatibilitySetting1 = new CompatibilitySetting() { Name = CompatSettingNameValues.CompatibilityMode, Uri = "http://schemas.microsoft.com/office/word", Val = "14" };
            CompatibilitySetting compatibilitySetting2 = new CompatibilitySetting() { Name = CompatSettingNameValues.OverrideTableStyleFontSizeAndJustification, Uri = "http://schemas.microsoft.com/office/word", Val = "1" };
            CompatibilitySetting compatibilitySetting3 = new CompatibilitySetting() { Name = CompatSettingNameValues.EnableOpenTypeFeatures, Uri = "http://schemas.microsoft.com/office/word", Val = "1" };
            CompatibilitySetting compatibilitySetting4 = new CompatibilitySetting() { Name = CompatSettingNameValues.DoNotFlipMirrorIndents, Uri = "http://schemas.microsoft.com/office/word", Val = "1" };

            compatibility.Append(compatibilitySetting1);
            compatibility.Append(compatibilitySetting2);
            compatibility.Append(compatibilitySetting3);
            compatibility.Append(compatibilitySetting4);

            Rsids rsids = new Rsids();
            var rsidRoot = new RsidRoot() { Val = "00B1753A" };
            var rsid65 = new Rsid() { Val = "00342720" };
            var rsid66 = new Rsid() { Val = "00B1753A" };
            var rsid67 = new Rsid() { Val = "00CE3A96" };

            rsids.Append(rsidRoot);
            rsids.Append(rsid65);
            rsids.Append(rsid66);
            rsids.Append(rsid67);

            settings.Append(compatibility);
            settings.Append(rsids);

            part.Settings = settings;
        }
    }
}
